package top.ed333.mcplugins.configurableevents;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import top.ed333.mcplugins.configurableevents.handler.Handler;
import top.ed333.mcplugins.configurableevents.handler.HandlerManager;
import top.ed333.mcplugins.configurableevents.handler.HandlerRegistry;
import top.ed333.mcplugins.configurableevents.handler.PkgManager;

import java.io.File;

public final class ConfigurableEvents extends JavaPlugin {
    private static final Listener listener = new Listener() {};

    @Override
    public void onEnable() {
        Inner.INSTANCE = this;

        DependenciesResolver.resolveDependencies();
        ConfigManager.initConfig();
        PkgManager.initPackages();
        loadHandlers();

        // register
        for (Handler handler : HandlerManager.getHandlers()) {
            HandlerRegistry.registerHandler(handler, listener, this);
        }

        // bstats
        int pluginId = 20612;
        final Metrics metrics = new Metrics(this, pluginId);
        final Metrics.SingleLineChart chart = new Metrics.SingleLineChart("handlerscount", HandlerManager::getLoadedHandlerCount);
        metrics.addCustomChart(chart);
    }

    @Override
    public boolean onCommand(CommandSender sender, @NotNull Command command, String label, String[] args) {
        if (command.getName().equals("configurableevents")) {
            if (args.length == 1 && args[0].equals("reload")) {
                if (sender.hasPermission("configurableevents.reload")) {
                    ConsoleSender.info("&a重载插件...");
                    HandlerList.unregisterAll(listener);
                    reloadConfig();
                    loadHandlers();
                    for (Handler handler : HandlerManager.getHandlers()) {
                        HandlerRegistry.registerHandler(handler, listener, this);
                    }
                } else {
                    sender.sendMessage("你没有权限执行这个命令.");
                }
            } else {
                sender.sendMessage("/configurableevents reload");
            }
        }
        return true;
    }

    private void loadHandlers() {
        File handlerCfgFolder = new File(getDataFolder(), "handlers");
        File[] files = handlerCfgFolder.listFiles(path -> path.getName().endsWith(".yml"));
        if (files != null) {
            HandlerManager.initHandlers(files);
            ConsoleSender.info("&a加载了 {0} 个事件处理器.", HandlerManager.getLoadedHandlerCount());
        } else {
            ConsoleSender.info("&a加载了 0 个事件处理器.");
        }
    }

    @Override
    public void onDisable() {
        HandlerList.unregisterAll(listener);
    }

    private static final class Inner {
        private static ConfigurableEvents INSTANCE;
    }

    public static ConfigurableEvents getInstance() {
        return Inner.INSTANCE;
    }
}
